
$(function() {
    validateRule();

});

$.validator.setDefaults({
    submitHandler: function() {
    	register();
    }
});

function register() {
    $.modal.loading($("#btnSubmit").data("loading"));
    var loginName = $.common.trim($("input[name='loginName']").val());
    var oldPassword = $.common.trim($("input[name='oldPassword']").val());
    var newPassword = $.common.trim($("input[name='newPassword']").val());
    var confirmPassword = $.common.trim($("input[name='confirmPassword']").val());

   // $.operate.save(ctx + "system/user/profile/resetPwd", $('#form-user-resetPwd').serialize());
    $.ajax({
        type: "post",
        url: ctx + "register",
        data: {
            "loginName": loginName,
            "oldPassword": oldPassword,
            "newPassword": newPassword,
            "confirmPassword": confirmPassword

        },
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
                alert( r.msg==null ? ('修改密码成功'):(r.msg));
                location.href = ctx + 'login';
            } else {
                alert( r.msg==null ? ('修改密码失败'):(r.msg));
                location.href = ctx + 'register';
            	// $.modal.closeLoading();
            	// $('.imgcode').click();
            	// $(".code").val("");
            	// $.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#registerForm").validate({
        rules: {
            loginName: {
                required: true,
                minlength: 6
            },
            oldPassword: {
                required: true,
                minlength: 12
            },
            confirmPassword: {
                required: true,
                equalTo: "[name='newPassword']"
            }
        },
        messages: {
            loginName: {
                required: icon + "请输入您的用户名",
                minlength: icon + "用户名不能小于6个字符"
            },
            password: {
            	required: icon + "请输入您的密码",
                minlength: icon + "密码不能小于12个字符",
            },
            confirmPassword: {
                required: icon + "请再次输入您的密码",
                equalTo: icon + "两次密码输入不一致"
            }
        }
    })
}
